class ChapterList < Gtk::ScrolledWindow

	BOOK_OPEN = Gdk::Pixbuf.new("#{DIR}/src/book_open.png")
	BOOK_CLOSED = Gdk::Pixbuf.new("#{DIR}/src/book_closed.png")

	attr_reader :current_chapter

	def initialize
		super()
		self.hscrollbar_policy = Gtk::POLICY_NEVER
		self.vscrollbar_policy = Gtk::POLICY_AUTOMATIC
		@vbox = Gtk::VButtonBox.new
		@vbox.layout_style = Gtk::ButtonBox::START
		@current_chapter = 0

		@menu = Gtk::Menu.new
		@menu.append(@move_up = Gtk::ImageMenuItem.new(Gtk::Stock::GO_UP))
		@menu.append(@move_down = Gtk::ImageMenuItem.new(Gtk::Stock::GO_DOWN))
		@menu.append(Gtk::SeparatorMenuItem.new)
		@menu.append(@delete = Gtk::ImageMenuItem.new(Gtk::Stock::DELETE))
		@menu.show_all

		@move_up.signal_connect('activate') do
			$main.book.chapters[@n], $main.book.chapters[@n-1] = $main.book.chapters[@n-1], $main.book.chapters[@n]
			refresh_buttons
			self.set_current_chapter(@n-1, true)
		end
		@move_down.signal_connect('activate') do
			$main.book.chapters[@n], $main.book.chapters[@n+1] = $main.book.chapters[@n+1], $main.book.chapters[@n]
			refresh_buttons
			self.set_current_chapter(@n+1, true)
		end
		@delete.signal_connect('activate') do
			dialog = Gtk::MessageDialog.new($main, Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
											Gtk::MessageDialog::WARNING, Gtk::MessageDialog::BUTTONS_YES_NO,
											_('Are you sure?'))
			dialog.secondary_text = _('Are you sure you want to delete this chapter? All its text will be lost!')
			dialog.default_response = Gtk::Dialog::RESPONSE_YES
			if dialog.run == Gtk::Dialog::RESPONSE_YES
				if @n == @current_chapter
					if @n == 0
						self.current_chapter += 0
					else
						self.current_chapter -= 1
					end
				end
				$main.book.chapters.delete_at(@n)
				refresh_buttons
				self.set_current_chapter(@n-1, true)
			end
			dialog.destroy
		end

		refresh_buttons
		
		self.add_with_viewport(@vbox)
		self.shadow_type = self.child.shadow_type = Gtk::SHADOW_NONE
	
		apply_config
		self.show_all
	end

	def current_chapter=(n)
		set_current_chapter(n)
	end

	def set_current_chapter(n, no_save=false)
		n = $main.book.chapters.length - 1 if n > $main.book.chapters.length - 1
		n = 0 if n < 0
		$main.book.chapters[@current_chapter].text = $main.text.formatted_text unless no_save
		@buttons[@current_chapter].child.children[0].children[0].pixbuf = BOOK_CLOSED
		@buttons[n].child.children[0].children[0].pixbuf = BOOK_OPEN
		@current_chapter = n
		$main.text.formatted_text = $main.book.chapters[n].text
		$main.dirty = false
	end

	def refresh_buttons
		@vbox.hide
		@vbox.each { |w| @vbox.remove(w) }
		@buttons = []
		
		if $main and $main.book
			n = 0
			$main.book.chapters.each do |chapter|
				b = Gtk::Button.new.set_relief(Gtk::RELIEF_NONE)
				b.instance_variable_set(:@n, n)
				align = Gtk::Alignment.new(0, 0.5, 1, 1)
				hbox = Gtk::HBox.new(false, 6)
				if @current_chapter == n
					hbox.pack_start(Gtk::Image.new(BOOK_OPEN), false, false)
				else
					hbox.pack_start(Gtk::Image.new(BOOK_CLOSED), false, false)
				end
				label = Gtk::Label.new(chapter.name).set_xalign(0)
				label.ellipsize = Pango::Layout::ELLIPSIZE_END
				hbox.pack_start(label)
				align << hbox
				b << align
				b.tip = chapter.name
				@buttons << b
				b.signal_connect('clicked') do |w|
					self.current_chapter = w.instance_variable_get(:@n)
				end
				b.signal_connect('button_press_event') do |w, e|
					if e.button == 3
						@n = w.instance_variable_get(:@n)
						@move_up.sensitive = (@n != 0)
						@move_down.sensitive = (@n != ($main.book.chapters.length - 1))
						@delete.sensitive = ($main.book.chapters.length > 1)
						@menu.popup(nil, nil, e.button, e.time)
					end
				end
				@vbox.pack_start(b, false, false)
				n += 1
			end
		end

		b = Gtk::Button.new.set_relief(Gtk::RELIEF_NONE)
		align = Gtk::Alignment.new(0, 0.5, 0, 1)
		hbox = Gtk::HBox.new(false, 6)
		hbox.pack_start(Gtk::Image.new(Gtk::Stock::ADD, Gtk::IconSize::BUTTON))
		hbox.pack_start(Gtk::Label.new(_('New chapter...')).set_xalign(0))
		align << hbox
		b << align
		b.signal_connect('clicked') do
			dialog = Gtk::Dialog.new(_('Type chapter name'), $main, Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
									 [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_REJECT])
			dialog.vbox.pack_start(Gtk::Label.new(_('Please type the name of the new chapter:')).set_xalign(0))
			dialog.vbox.pack_start(entry = Gtk::Entry.new)
			dialog.vbox.spacing = 6
			dialog.border_width = 12
			dialog.vbox.show_all
			dialog.default_response = Gtk::Dialog::RESPONSE_ACCEPT
			if dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
				$main.book.chapters[@current_chapter].text = $main.text.formatted_text
				$main.book.chapters << Chapter.new(entry.text)
				refresh_buttons
				self.current_chapter = $main.book.chapters.length - 1
			end
			dialog.destroy
		end
		@vbox.pack_start(b, false, false)

		@vbox.show_all
	end

	def apply_config
		self.modify_bg(Gtk::STATE_NORMAL, $conf.bg)
		self.child.modify_bg(Gtk::STATE_NORMAL, $conf.bg)
	end

end
